package com.nsgf.library.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class SPUtil {
    /**
     * spMap 用于存放SharedPreferences的集合
     *
     * @author Desmond 2014-10-15 下午2:02:59
     */
    private static final Map<String, SoftReference<SharedPreferences>> SP_MAP = new HashMap<String, SoftReference<SharedPreferences>>();

    public static String defaultPreferenceName;


    /**
     * 获取SharedPreferences，如果在spMap中存在，就取出，否则就新建
     *
     * @param name SharedPreferences 名字
     * @return SharedPreferences
     * @author Desmond 2014-10-15 下午2:01:56
     */
    public static SharedPreferences getSp(String name, Context context) {
        defaultPreferenceName = context.getPackageName() + "_preferences";
        if (name == null)
            name = defaultPreferenceName;

        if (!SP_MAP.containsKey(name) || SP_MAP.get(name).get() == null) {
            // 软引用
            SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
            SP_MAP.put(name, new SoftReference<SharedPreferences>(sp));
        }
        return SP_MAP.get(name).get();
    }

    public static void put(String spName, String key, String value, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putString(key, value).commit();
    }

    public static void putInt(String spName, String key, int value, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putInt(key, value).commit();
    }

    public static void putBoolean(String spName, String key, boolean value, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putBoolean(key, value).commit();
    }

    public static void putLong(String spName, String key, long value, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putLong(key, value).commit();
    }

    public static void put(String spName, String key, boolean value, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putBoolean(key, value).commit();
    }

    public static void put(String spName, String key, Set<String> values, Context context) {
        SharedPreferences sp = getSp(spName, context);
        sp.edit().putStringSet(key, values).commit();
    }

    public static String get(String spName, String key, String defValue, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getString(key, defValue);
    }

    public static int getInt(String spName, String key, int defValue, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getInt(key, defValue);
    }

    public static boolean getBoolean(String spName, String key, boolean defValue, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getBoolean(key, defValue);
    }

    public static Set<String> get(String spName, String key, Set<String> defValues, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getStringSet(key, defValues);
    }

    public static boolean contains(String spName, String key, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.contains(key);
    }

    public static long getLong(String spName, String key, long defValue, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getLong(key, defValue);
    }

    public static boolean get(String spName, String key, boolean defValue, Context context) {
        SharedPreferences sp = getSp(spName, context);
        return sp.getBoolean(key, defValue);
    }

    public static void clear(String spName, String key, Context context) {
        getSp(spName, context).edit().remove(key).commit();
    }

    public static void clear(String spName, Context context) {
        getSp(spName, context).edit().clear().commit();
    }

    public static void remove(String key, Context context) {
        getSp(defaultPreferenceName, context).edit().remove(key).commit();
    }

    public static void put(String key, String value, Context context) {
        put(null, key, value, context);
    }

    public static void putInt(String key, int value, Context context) {
        putInt(null, key, value, context);
    }

    public static void putLong(String key, long value, Context context) {
        putLong(null, key, value, context);
    }

    public static void put(String key, boolean value, Context context) {
        put(null, key, value, context);
    }

    public static void put(String key, Set<String> values, Context context) {
        put(null, key, values, context);
    }

    public static String get(String key, String defValue, Context context) {
        return get(null, key, defValue, context);
    }

    public static int getInt(String key, int defValue, Context context) {
        return getInt(null, key, defValue, context);
    }

    public static Set<String> get(String key, Set<String> defValues, Context context) {
        return get(null, key, defValues, context);
    }

    public static boolean contains(String key, Context context) {
        return contains(null, key, context);
    }

    public static long getLong(String key, long defValue, Context context) {
        return getLong(null, key, defValue, context);
    }

    public static boolean get(String key, boolean defValue, Context context) {
        return get(null, key, defValue, context);
    }
}
